package com.skytech.component.crawler.web;


import com.alibaba.fastjson.JSONObject;
import com.skytech.component.crawler.scratch.CrawlerUrlDAO;
import com.skytech.component.crawler.scratch.TargetRules;
import com.skytech.component.crawler.scratch.TargetRulesDAO;
import com.skytech.component.crawler.task.CrawlerTask;
import com.skytech.component.crawler.task.NewsListPageOperation;
import com.skytech.component.crawler.task.TaskManager;
import com.skytech.component.crawler.quartz.UpdateCronTask;
import com.skytech.component.crawler.util.HttpClientPost;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class TestController {
    @Autowired
    private TargetRulesDAO targetRulesDAO;
    @Autowired
    private CrawlerUrlDAO crawlerUrlDAO;
    @Autowired
    private CrawlerTask crawlerTask;

    @Value("${chrome.driver.path}")
    private String driverPath;

    private static final Logger log = LoggerFactory.getLogger(TestController.class);

    @GetMapping("test/part/errorpage/{id}")
    public String testPartErrorPage(@PathVariable String id){
        List<String> ids = Arrays.asList(id);
        List<TargetRules> ruleList = targetRulesDAO.findAll().stream().filter(l -> ids.contains(l.getId())).collect(Collectors.toList());
        TaskManager taskManager = new TaskManager(ruleList,driverPath,new NewsListPageOperation(crawlerUrlDAO));
        taskManager.start();
       // crawlerTask.startChromeTask();
        /*singleChromeTask.start(ruleList,crawlerUrlDAO);*/
        return "开始测试任务";

    }

    @GetMapping("test/task/all")
    public String testAll(){
        /*List<TargetRules> ruleList = targetRulesDAO.findAll();
        ruleList.sort(Comparator.comparing(TargetRules::getId));
        singleChromeTask.start(ruleList,crawlerUrlDAO);*/
        crawlerTask.startChromeTask();
        return "开始测试任务";

    }
    @GetMapping("test/date")
    public List<String> getDate(){
        String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return Arrays.asList(format,new Date().toString());
    }

    @Autowired
    private HttpClientPost httpClientPost;

    @PostMapping("test/http")
    public JSONObject httpClient(@RequestBody JSONObject jsonObject){
       return  httpClientPost.send1(jsonObject);
    }

    @PostMapping("test/http1")
    public JSONObject httpClient1(@RequestBody JSONObject jsonObject) throws Exception {
        return  httpClientPost.send(jsonObject);
    }




}
